#!/bin/sh
#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
set +x

# Copyright (c) Huawei Technologies Co., Ltd. 2012-2023. All rights reserved.
# Description: get oracle info return info in result file
# Arguments: SCRIPT_ROOT_PATH=$1 PID=$2
# Returns: 0 is success, other is fail, result_file return sid info


SCRIPT_ROOT_PATH=$1
PID=$2
PARAM_NUM=$3

. "${SCRIPT_ROOT_PATH}"/oracle_sbin_func.sh

TMP_FILE="${STMP_PATH}/oracle_info_tmp_${PID}.txt"

PARAM_CONTENT=`read_input_param "${PARAM_NUM}"`

DB_NAME=`get_value "${PARAM_CONTENT}" AppName`
ORA_DB_USER=`get_value "${PARAM_CONTENT}" OracleInstallUser`
ORA_GRID_USER=`get_value "${PARAM_CONTENT}" GridInstallUser`
ORA_USER_NAME=`get_value "${PARAM_CONTENT}" User`
ORA_DB_PASSWORD=`get_value "${PARAM_CONTENT}" Password`
ORACLE_BASE=`get_value "${PARAM_CONTENT}" AccessOracleBase`
ORACLE_HOME=`get_value "${PARAM_CONTENT}" AccessOracleHome`
SUB_TYPE=`get_value "${PARAM_CONTENT}" SubType`


check_oracle_base_and_home

#get oracle user info
get_oracle_user
if [ -z "${ORA_DB_USER}" ] || [ -z "${ORA_DB_GROUP}" ]; then
    log_error "Get oracle user failed"
    exit "${RETURN_INVALID}"
fi
get_oracle_user_shell_type
env_file_list=`get_user_env_file "${ORA_DB_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_ORACLE_ENV=`generate_export_cmd "$env_file_list" "oracle"`
if [ $? -ne 0 ] || [ -z "$EXPORT_ORACLE_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi

# check oracle install
check_oracle_is_install

# check oracle install type
check_oracle_install_type
log_info "check_oracle_install_type DBISCLUSTER ${DBISCLUSTER}, SUB_TYPE ${SUB_TYPE}"
if [ "$DBISCLUSTER" -eq 1 ] && [ "${SUB_TYPE}" = "Oracle" ]; then
    log_error "check_oracle_install_type failed"
    exit "${ERROR_CHECK_ORACLE_INSTALL_TYPE}"
fi

#get oracle base path
get_oracle_base_path

#get oracle home path
get_oracle_home_path

#get oracle version
get_oracle_version

# instance list file get_oracle_instance_list function return value file
sid_tmp_file="$STMP_PATH/ins_info_${PID}.txt"

#get oracle instance info
get_oracle_instance_list "${sid_tmp_file}"
if [ "$DBISCLUSTER" -gt 0 ]; then
    is_asm=1
else
    is_asm=0
fi
while read instance_name; do
    if [ -z "${instance_name}" ]; then
        continue
    fi
    if [ "`rd_substr "${instance_name}" 1 1`" = "+" ]; then
        continue
    fi
    get_instance_auth_type "${instance_name}"
    auth_type=$?
    if [ ${auth_type} -eq 2 ]; then
        log_warn "Get oracle auth type is failed"
    fi
    get_instance_process_status "${instance_name}"
    instance_status=$?
    if [ ${instance_status} -eq 2 ]; then
        log_warn "From process info get instance [${instance_name}] status is fail"
    fi
    if [ $instance_status -eq "$INSTANCE_ONLINE" ]; then
        tdb_name=`get_database_name "${instance_name}" "${ORA_USER_NAME}" "${ORA_DB_PASSWORD}" "${auth_type}"`
        tdb_name=`echo "${tdb_name}" | tr [A-Z] [a-z]`
        DB_NAME=`echo "${DB_NAME}" | tr [A-Z] [a-z]`
         if [ "$DB_NAME" = "$tdb_name" ] ; then
              log_info "Oracle version: ${ORA_VERSION}, instance_name: ${instance_name}, db_name: ${tdb_name}; status: ${instance_status}; auth_type: ${auth_type},db_type: ${DBISCLUSTER}; home_env: ${IN_ORACLE_HOME}; base_env: ${IN_ORACLE_BASE}"
              echo "${ORA_VERSION};${instance_name};${tdb_name};${instance_status};${is_asm};${auth_type};${DBISCLUSTER};${IN_ORACLE_HOME};${IN_ORACLE_BASE}" >> "${RESULT_FILE}"
              break
         fi
    fi

done < "${sid_tmp_file}"

delete_file "${TMP_FILE}" "${sid_tmp_file}"

#chmod 640 RESULT_FILE
if [ -f "${RESULT_FILE}" ]; then
    chmod 640 "${RESULT_FILE}"
fi
log_info "Get oracle info success"
